package com.cmcm.gezihua.myapplication;

import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.cmcm.aoplib.NetPermissionCheck;
import com.cmcm.aoplib.NetPermissionCheckMgr;
import com.cmcm.aoplib.SecurityCheckAnnotation;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
    static {
        NetPermissionCheckMgr.getIns().setNetPermissionCheck(new NetPermissionCheck() {
            @Override
            public boolean isNetPermissionAcceptted() {
                return false;
            }
        });
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {

            }
        });


    }

    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test("111111111111");
//        test("");


        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        disablePermission();;
                    }
                }.start();

            }
        });
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startThread();
            }
        });

    }

    private void disablePermission() {
        PackageManager packageManager = getPackageManager();
        packageManager.removePermission("android.permission.INTERNET");
    }

    private void startThread(){
        Log.e("DemoAspect","call test");
        new Thread(){
            @Override
            public void run() {
                super.run();
                openNet();
            }
        }.start();

    }
    @SecurityCheckAnnotation(declaredPermission="android.permission.READ_PHONE_STATE")
    public void checkPhoneState(){
        if(checkPermission("android.permission.READ_PHONE_STATE") == false){
            Log.e(TAG,"have no permission to read phone state");
            return;
        }
        Log.e(TAG,"Read Phone State succeed");
        return;
    }
    private boolean checkPermission(String permissionName){
        try{
            PackageManager pm = getPackageManager();
            int nret = pm.checkPermission(permissionName,getPackageName());
            return nret == PackageManager.PERMISSION_GRANTED;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    private void test(String s){


    }



    private void openNet(){
        try {
            URL url = new URL("https://www.baidu.com/");
            URLConnection urlConnection = url.openConnection();
            Object content = urlConnection.getContent();
            Log.e("suj",content.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
